const PREFIX = 'toast_'
const ToastActionType = {
  show_toast: PREFIX + 'toggle_toast',
  hide_toast: PREFIX + 'hide_toast'
}
var timer = null
const showToast = (text, time) => {
  return (dispatch, getState) => {
    dispatch(_showToast(text))
    timer && clearTimeout(timer)
    timer = setTimeout(() => {
      dispatch(_hideToast())
    }, time)
  }
}

const _showToast = (text) => {
  return {
    type: ToastActionType.show_toast,
    text: text
  }
}
const _hideToast = () => {
  return {
    type: ToastActionType.hide_toast
  }
}

module.exports = {
  ToastActionType,
  showToast
}
